﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Windows;
using TriviaViewModels.ServiceReference1;
using System.Windows.Forms;
using GalaSoft.MvvmLight.Command;



namespace TriviaViewModels
{
    public class GameViewModelWizard:WizardViewBase
    {
        int clueCommandExacute = 0;
        EntryViewModelWizard _entranceViewModel;

        List<int> _timeScore = new List<int>();
        DispatcherTimer timer;

        string _userName;
        string _password;

        int timeInt;
        public void StartTimer()
        {
            timer = new DispatcherTimer();
            timeInt = 0;
            timer.Tick += new EventHandler(moveTime);
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Start();
        }

        private void moveTime(object sender, EventArgs e)
        {
            timeInt++;
            if (timeInt < 10)
            {
                Time = "00 : 0" + timeInt.ToString();
            }
            else
            {
                Time = "00: " + timeInt.ToString();
            }
            if (timeInt > 59)
            {
                MoveToNextQuestion();
            }
        }
        private Visibility _isClueVisible;
        public Visibility IsClueVisible
        {
            get { return _isClueVisible; }
            set
            {
                if (_isClueVisible != value)
                {
                    _isClueVisible = value;
                    RaisePropertyChanged("IsClueVisible");
                }
            }
        }

        private Visibility _isLableVisible;
        public Visibility IsLableVisible
        {
            get { return _isLableVisible; }
            set
            {
                if (_isLableVisible != value)
                {
                    _isLableVisible = value;
                    RaisePropertyChanged("IsLableVisible");
                }

            }
        }

        private Visibility _isGameFinished;
        public Visibility IsGameFinished
        {
            get { return _isGameFinished; }
            set
            {
                if (_isGameFinished != value)
                {
                    _isGameFinished = value;
                    RaisePropertyChanged("IsGameFinished");
                }
            }
        }

        private Visibility _isAnswerVisible;
        public Visibility IsAnswersVisible
        {
            get { return _isAnswerVisible; }
            set
            {
                if (_isAnswerVisible != value)
                {
                    _isAnswerVisible = value;
                    RaisePropertyChanged("IsAnswersVisible");
                }
            }
        }

        private string _time;
        public string Time
        {
            get { return _time; }
            set
            {
                if (_time != value)
                {
                    _time = value;
                    RaisePropertyChanged("Time");
                }

            }
        }

        private int _questionIndex = 0;

        private string _isTheRightAnswereContent;
        public string IsTheRightAnswereContent
        {
            get { return _isTheRightAnswereContent; }
            set
            {
                if (_isTheRightAnswereContent != value)
                {

                    _isTheRightAnswereContent = value;
                    RaisePropertyChanged("IsTheRightAnswereContent");
                }
            }
        }

       

        public GameViewModelWizard()
        {
            StartTimer();
            IsGameFinished = Visibility.Collapsed;
            _gameQuestions = new List<ServiceReference1.QuestionCalss>();
            _gameAnswers = new List<ServiceReference1.AnswerCalss>();
            QuestionAnswers = new List<ServiceReference1.AnswerCalss>();
            IsClueVisible = Visibility.Collapsed;
            IsLableVisible = Visibility.Collapsed;
        }
        private List<AnswerCalss> _gameAnswers;
        private List<QuestionCalss> _gameQuestions;

        private ServiceReference1.QuestionCalss _currentQuestion;
        public ServiceReference1.QuestionCalss CurrentQuestion
        {
            get { return _currentQuestion; }
            set
            {
                if (_currentQuestion != value)
                {
                    _currentQuestion = value;
                    RaisePropertyChanged("CurrentQuestion");
                }
            }
        }

        private List<AnswerCalss> _questionAnswers;
        public List<AnswerCalss> QuestionAnswers
        {
            get { return _questionAnswers; }
            set
            {
                if (_questionAnswers != value)
                {
                    _questionAnswers = value;
                    RaisePropertyChanged("QuestionAnswers");
                }
            }
        }

        private AnswerCalss _selectedAnswer;
        public AnswerCalss SelectedAnswer
        {
            get { return _selectedAnswer; }
            set
            {
                var proxy = new GameServiceClient();
                if (_selectedAnswer != value)
                {
                    _selectedAnswer = value;
                    proxy.IsTheAnswerRightCompleted += (s, e) =>
                    {
                        IsLableVisible = Visibility.Visible;
                        IsAnswersVisible = Visibility.Hidden;
                        SelectedAnswer.IsRightAnswer = e.Result;

                        RaisePropertyChanged("SelectedAnswer");

                    };
                    if (_selectedAnswer != null && _selectedAnswer.AnswerString != null)
                        proxy.IsTheAnswerRightAsync(SelectedAnswer.Id);
                    else
                        RaisePropertyChanged("SelectedAnswer");
                }


            }
        }




        public void SetQuestionsAndAnswers(string difficulty, string topic)
        {
            using (var proxy = new ServiceReference1.GameServiceClient())
            {
                _gameQuestions = proxy.GetQusetions(difficulty, topic).ToList();
                var qstionIDS = _gameQuestions.Select(item => item.Id);
                _gameAnswers = proxy.GetAnsweres(qstionIDS.ToArray()).ToList();
                if (_gameQuestions.Count > 0 && _gameAnswers.Count > 0)
                {
                    CurrentQuestion = _gameQuestions[_questionIndex];
                    QuestionAnswers = _gameAnswers.Where(item => item.QuestionId == CurrentQuestion.Id).ToList();
                }
            }
        }



        private RelayCommand _nextQuestionCommand;

        public RelayCommand NextQuestionCommand
        {
            get
            {
                if (_nextQuestionCommand == null)
                    _nextQuestionCommand = new RelayCommand(MoveToNextQuestion);

                return _nextQuestionCommand;
            }
            set
            {
                if (_nextQuestionCommand != value)
                {
                    _nextQuestionCommand = value;
                    RaisePropertyChanged("NextQuestionCommand");
                }
            }
        }

        private RelayCommand _showClueCommand;

        public RelayCommand ShowClueCommand
        {
            get
            {
                if (_showClueCommand == null)
                {
                    _showClueCommand = new RelayCommand(() =>
                    {
                        clueCommandExacute++;
                        if (clueCommandExacute % 2 == 0)
                            IsClueVisible = Visibility.Collapsed;
                        else
                            IsClueVisible = Visibility.Visible;

                    });
                } return _showClueCommand;
            }
            set
            {
                if (_showClueCommand != value)
                {
                    _showClueCommand = value;
                    RaisePropertyChanged("ShowClueCommand");
                }
            }
        }

        public void MoveToNextQuestion()
        {
            IsAnswersVisible = Visibility.Visible;
            IsLableVisible = Visibility.Hidden;

            var proxy = new ServiceReference1.GameServiceClient();

            if (_questionIndex + 1 < _gameQuestions.Count)
            {
                if (SelectedAnswer != null)
                {
                    proxy.IsTheAnswerRightCompleted += (s, e) =>
                    {
                        SelectedAnswer.IsRightAnswer = e.Result;
                        if (SelectedAnswer.IsRightAnswer == true)
                            _timeScore.Add(timeInt);
                        _questionIndex++;
                        timer.Stop();
                        StartTimer();
                        CurrentQuestion = _gameQuestions[_questionIndex];
                        QuestionAnswers = _gameAnswers.Where(item => item.QuestionId == CurrentQuestion.Id).ToList();
                    }; proxy.IsTheAnswerRightAsync(SelectedAnswer.Id);
                }
                else
                {
                    _questionIndex++;
                    CurrentQuestion = _gameQuestions[_questionIndex];
                    QuestionAnswers = _gameAnswers.Where(item => item.QuestionId == CurrentQuestion.Id).ToList();
                }



            }

            else if (_questionIndex + 1 == _gameQuestions.Count)
            {
                int score;
                if (SelectedAnswer != null)
                {
                    proxy.IsTheAnswerRightCompleted += (s, e) =>
                    {
                        SelectedAnswer.IsRightAnswer = e.Result;
                        if (SelectedAnswer.IsRightAnswer == true)
                            _timeScore.Add(timeInt);
                        if (_userName != null && _password != null)
                            score = proxy.CalcAndGetScore(_timeScore.ToArray(), _userName, _password);
                        else
                            score = proxy.CalcAndGetScore(_timeScore.ToArray(), null, null);

                        System.Windows.Forms.MessageBox.Show("tour score is " + score.ToString());
                        IsAnswersVisible = Visibility.Collapsed;
                        IsGameFinished = Visibility.Visible;

                    };
                    proxy.IsTheAnswerRightAsync(SelectedAnswer.Id);
                }




            }
        }

        internal void SetUser(string UsereNam, string Password)
        {
            bool exists;
            _userName = UsereNam;
            _password = Password;
            using (var proxy = new GameServiceClient())
            {
                exists = proxy.InsertUser(_userName, _password);
            }
            if (exists)
                MessageBox.Show("welcome back");
            else
                MessageBox.Show("welcome");




        }
    }
}
